{SysSrv} = require "./SysSrv"

exports.MainFrameSrv = new Class(
  Extends: SysSrv
  #获得菜单对应的孩子
  menuCld: (reqOpt,menuId)->
    t = this
    emp = t.session.emp
    sql = """
    select m.*
    from menu m 
    join menu_tree_path mp 
      on m.id=mp.descendant
    where 
      mp.ancestor=$1
      and mp.path_length=1
      and m.enable=true
      and exists(
        select rg.* 
        from role_rig rg
        left join role_emp re
          on re.role_id=rg.role_id
        left join emp e
          on re.emp_id=e.id
        where rg.menu_id=m.id
          and e.id=$2
          and rg.rg=true
      )
    order by m.sort_num asc
    """
    rltSet = yield t.callSql reqOpt,sql,[menuId,emp.id]
    rltSet = rltSet.rows
    rltSet
  #获得菜单的根节点,可能有很多个
  menuRoot: (reqOpt)->
    t = this
    emp = t.session.emp
    sql = """
    select m.*
    from menu m 
    where 
      m.is_root=true
      and m.enable=true
      and exists(
        select rg.* 
        from role_rig rg
        left join role_emp re
          on re.role_id=rg.role_id
        left join emp e
          on re.emp_id=e.id
        where rg.menu_id=m.id
          and e.id=$1
          and rg.rg=true
      )
    order by m.sort_num asc
    """
    rltSet = yield t.callSql reqOpt,sql,[emp.id]
    rltSet = rltSet.rows
    rltSet
)